package com.hx.test.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.Map;


/**
 * Created by hyl on 19/4/21.
 * @author 华云龙
 */
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef="entityManagerFactoryPrimary",
        transactionManagerRef="transactionManagerPrimary",
        //设置Repository所在位置
        basePackages= { "com.hx.test.dao.book" })
public class PrimaryConfig {

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private HibernateProperties hibernateProperties;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private JpaProperties jpaProperties;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Autowired
    private DataSource dataSource;

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Primary
    @Bean(name = "entityManagerPrimary")
    public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
        return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
    }



    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Primary
    @Bean(name = "entityManagerFactoryPrimary")
    public LocalContainerEntityManagerFactoryBean
    entityManagerFactoryPrimary(
            EntityManagerFactoryBuilder builder
    ) {
        Map<String,Object> properties= hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings());
        return builder
                .dataSource(dataSource)
                // 【3】这里是实体类的包路径
                .packages("com.hx.test.entity.book")
                // 这里写成唯一的就可以了，具体不懂(也许是用作下面之用)
                //Closing JPA EntityManagerFactory for persistence unit 'primary'
                .persistenceUnit("primary")
                // 【4】
                .properties(properties)
                .build();
    }

    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Primary
    @Bean(name = "transactionManagerPrimary")
    public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
        return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
    }
}
